package cn.wx.pet.service.impl;

import cn.wx.basic.service.impl.BaseServiceImpl;
import cn.wx.org.domain.Department;
import cn.wx.pet.domain.Pet;
import cn.wx.pet.domain.PetType;
import cn.wx.pet.mapper.PetTypeMapper;
import cn.wx.pet.service.IPetService;
import cn.wx.pet.service.IPetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType>  implements IPetTypeService {

    @Autowired
    private PetTypeMapper petTypeMapper;
    @Override
    public List<PetType> typeTree() {

        //获取所有对象
        List<PetType> all = petTypeMapper.loadAll();

        //创建一个map，通过id  K值 获取 对象
        HashMap<Long, PetType> map = new HashMap<>();

        //循环所有对象，存入到map中
        for (PetType petType : all) {
            map.put(petType.getId(),petType);
        }
        //前端需要得到的集合数据 - 部门树
        ArrayList<PetType> typeTree = new ArrayList<>();

        for (PetType petType : all) {
            //如果patent_id==null表示为顶级部门
            if(petType.getPid()==null){
                typeTree.add(petType);
            }else {
                Long Pid = petType.getPid();  //如果不是，把循环当前的parent_id接收一下
                PetType parentPetType = map.get(Pid);  //获取map里面当前的循环的谁的parent_id是这个parent_id 的对象
                parentPetType.getChildren().add(petType);  //把这个子集部门封装到Children中

            }
        }
        return typeTree;
    }




    }

